package com.datagateway.model;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonProperty;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * 处理后的数据模型
 * 用于存储经过Groovy脚本转换后的数据
 * 
 * @author Data Gateway Team
 * @version 1.0.0
 */
public class ProcessedData {

    /**
     * 数据唯一标识
     */
    @JsonProperty("id")
    private String id;

    /**
     * 原始数据
     */
    @JsonProperty("originalData")
    private String originalData;

    /**
     * 转换后的数据
     */
    @JsonProperty("transformedData")
    private Map<String, Object> transformedData;

    /**
     * 数据来源主题
     */
    @JsonProperty("sourceTopic")
    private String sourceTopic;

    /**
     * 数据分区
     */
    @JsonProperty("partition")
    private Integer partition;

    /**
     * 数据偏移量
     */
    @JsonProperty("offset")
    private Long offset;

    /**
     * 处理时间
     */
    @JsonProperty("processTime")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS")
    private LocalDateTime processTime;

    /**
     * 数据时间戳
     */
    @JsonProperty("dataTimestamp")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS")
    private LocalDateTime dataTimestamp;

    /**
     * 处理状态
     */
    @JsonProperty("status")
    private ProcessStatus status;

    /**
     * 错误信息（如果有）
     */
    @JsonProperty("errorMessage")
    private String errorMessage;

    /**
     * 处理耗时（毫秒）
     */
    @JsonProperty("processingTimeMs")
    private Long processingTimeMs;

    /**
     * 数据大小（字节）
     */
    @JsonProperty("dataSize")
    private Integer dataSize;

    /**
     * 数据内容（用于数据源存储）
     */
    @JsonProperty("content")
    private String content;

    /**
     * 时间戳（用于数据源存储）
     */
    @JsonProperty("timestamp")
    private Long timestamp;

    /**
     * 元数据（用于数据源存储）
     */
    @JsonProperty("metadata")
    private Map<String, Object> metadata;

    /**
     * 默认构造函数
     */
    public ProcessedData() {
        this.processTime = LocalDateTime.now();
        this.status = ProcessStatus.SUCCESS;
    }

    /**
     * 带参数的构造函数
     * 
     * @param id 数据ID
     * @param originalData 原始数据
     * @param transformedData 转换后数据
     * @param sourceTopic 来源主题
     */
    public ProcessedData(String id, String originalData, Map<String, Object> transformedData, String sourceTopic) {
        this();
        this.id = id;
        this.originalData = originalData;
        this.transformedData = transformedData;
        this.sourceTopic = sourceTopic;
        this.dataSize = originalData != null ? originalData.getBytes().length : 0;
    }

    // Getter和Setter方法
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getOriginalData() {
        return originalData;
    }

    public void setOriginalData(String originalData) {
        this.originalData = originalData;
        this.dataSize = originalData != null ? originalData.getBytes().length : 0;
    }

    public Map<String, Object> getTransformedData() {
        return transformedData;
    }

    public void setTransformedData(Map<String, Object> transformedData) {
        this.transformedData = transformedData;
    }

    public String getSourceTopic() {
        return sourceTopic;
    }

    public void setSourceTopic(String sourceTopic) {
        this.sourceTopic = sourceTopic;
    }

    public Integer getPartition() {
        return partition;
    }

    public void setPartition(Integer partition) {
        this.partition = partition;
    }

    public Long getOffset() {
        return offset;
    }

    public void setOffset(Long offset) {
        this.offset = offset;
    }

    public LocalDateTime getProcessTime() {
        return processTime;
    }

    public void setProcessTime(LocalDateTime processTime) {
        this.processTime = processTime;
    }

    public LocalDateTime getDataTimestamp() {
        return dataTimestamp;
    }

    public void setDataTimestamp(LocalDateTime dataTimestamp) {
        this.dataTimestamp = dataTimestamp;
    }

    public ProcessStatus getStatus() {
        return status;
    }

    public void setStatus(ProcessStatus status) {
        this.status = status;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public Long getProcessingTimeMs() {
        return processingTimeMs;
    }

    public void setProcessingTimeMs(Long processingTimeMs) {
        this.processingTimeMs = processingTimeMs;
    }

    public Integer getDataSize() {
        return dataSize;
    }

    public void setDataSize(Integer dataSize) {
        this.dataSize = dataSize;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public Long getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Long timestamp) {
        this.timestamp = timestamp;
    }

    public Map<String, Object> getMetadata() {
        return metadata;
    }

    public void setMetadata(Map<String, Object> metadata) {
        this.metadata = metadata;
    }

    /**
     * 处理状态枚举
     */
    public enum ProcessStatus {
        /**
         * 处理成功
         */
        SUCCESS,
        
        /**
         * 处理失败
         */
        FAILED,
        
        /**
         * 处理中
         */
        PROCESSING,
        
        /**
         * 跳过处理
         */
        SKIPPED
    }

    @Override
    public String toString() {
        return String.format("ProcessedData{id='%s', sourceTopic='%s', status=%s, " +
                           "processTime=%s, dataSize=%d, processingTimeMs=%d}",
                           id, sourceTopic, status, processTime, dataSize, processingTimeMs);
    }
}
